﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Coslen.Framework.Helpers;
using Coslen.TahorCRM.Security;

namespace Coslen.TahorCRM.UI.www.Controllers
{
    public class HomeController : TahorBaseController
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }

        private byte[] CreateCalenderPDF()
        {
            // This is the concatenation of the html pages
            StringBuilder html = new StringBuilder();

            html.AppendLine("<?xml version=\"1.0\"?>");
            html.AppendLine("<!DOCTYPE html>");
            html.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">");

            // Open Head Tag
            html.AppendLine("<head>");
            
            // Add Style Sheets
            html.AppendLine("<style type=\"text/css\">");

            TextReader trSite = new StreamReader(Server.MapPath(@"/Content/Site.css"));
            html.Append(trSite.ReadToEnd());
            trSite.Close();

            TextReader trFullCalendar = new StreamReader(Server.MapPath(@"/Content/fullcalendar.css"));
            html.Append(trFullCalendar.ReadToEnd());
            trFullCalendar.Close();

            html.AppendLine("</style>");

            // Close Head tag
            html.AppendLine("</head>");

            // Open Body Tag
            html.AppendLine("<body>");
            
            string tmpHtml = string.Empty;
            tmpHtml = RenderPartialViewToString("_CalenderView", null);

            html.Append(tmpHtml);

            // Close Body
            html.AppendLine("</body>");

            // CLose Html
            html.AppendLine("</html>");

            // 
            string rawHtml = html.ToString();

            FileHelper.SaveAsFile(rawHtml, @"c:\\temp\renderCalenderTest.html");

            byte[] pdfBytes = null;

            //if (paperSize == "A4")
            //{
            //    pdfBytes = PdfService.TransformToPDF(rawHtml, PdfPaperSize.PRN_VIS_PAPER_A4, PdfPageOrientation.LANDSCAPE);
            //}
            //else
            //{
            //    pdfBytes = PdfService.TransformToPDF(rawHtml, PdfPaperSize.PRN_VIS_PAPER_LETTER, PdfPageOrientation.LANDSCAPE);
            //}

            ////AsposeService.SaveBytesAsPdf(pdfBytes, @"D:\\temp\test.pdf");

            //// Transform it.
            //if (paperSize == "A4")
            //{
            //    pdfBytes = PdfService.TransformToInterlacedGuilotine(pdfBytes, 2, 1, PdfPaperSize.PRN_VIS_PAPER_A4, PdfPageOrientation.LANDSCAPE);
            //}
            //else
            //{
            //    pdfBytes = PdfService.TransformToInterlacedGuilotine(pdfBytes, 2, 1, PdfPaperSize.PRN_VIS_PAPER_LETTER, PdfPageOrientation.LANDSCAPE);
            //}

            return pdfBytes;
        }

        /// <summary>
        /// SOURCE: http://stackoverflow.com/questions/4344533/asp-net-mvc-razor-how-to-render-a-razor-partial-views-html-inside-the-controlle
        /// </summary>
        /// <param name="viewName"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                viewName = ControllerContext.RouteData.GetRequiredString("action");
            }

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }

        public ActionResult ServerSide404(int id)
        {
            this.HttpContext.AddError(new HttpException(404, "Invalid Id - " + id));
            return View();
        }

        public ActionResult AboutMe()
        {
            Dictionary<string, string> variables = new Dictionary<string, string>();

            variables.Add("LOGON_USER", Request.ServerVariables["LOGON_USER"]);
            variables.Add("AUTH_USER", Request.ServerVariables["AUTH_USER"]);
            variables.Add("Principal Type", HttpContext.User.GetType().ToString());
            variables.Add("Identity Type", HttpContext.User.Identity.GetType().ToString());
            variables.Add("Identity Name", HttpContext.User.Identity.Name);
            if (Request.LogonUserIdentity != null) variables.Add("LogonUserIdentity - Name", Request.LogonUserIdentity.Name);
            variables.Add("Current Date Time", DateTime.Now.ToString("dddd dd MMM yyyy HH:mm"));
            
            ViewBag.Variables = variables;

            var model = HttpContext.User as TahorPrincipal;

            return View(model);
        }
    }
}
